﻿Imports System.Xml
Public Class PopulateControls

    Public Sub PopulateTreeFromXML(ByRef control As vsTreeView, ByRef xmlDoc As Xml.XmlDocument)
        For Each node As XmlNode In xmlDoc.ChildNodes
            If node.Name = "xml" Then
                Continue For
            End If
            Dim name As String = ""
            Dim newNode As New TreeNode()

            Select Case node.Name
                Case "#text"
                    newNode.NodeFont = New Font(SystemFonts.MessageBoxFont, FontStyle.Italic)
                    newNode.ForeColor = Color.Red
                    name = node.Name & ": " & node.Value
                Case "#comment"
                    newNode.NodeFont = New Font(SystemFonts.MessageBoxFont, FontStyle.Italic)
                    newNode.ForeColor = Color.Gray
                    name = node.Name & ": " & node.Value
                Case "#cdata-section"
                    newNode.NodeFont = New Font(SystemFonts.MessageBoxFont, FontStyle.Italic)
                    newNode.ForeColor = Color.Red
                    name = node.Name & ": " & node.Value
                Case Else
                    If Not newNode Is Nothing Then
                        Dim AttributeList As String = ""
                        For Each Att As XmlAttribute In node.Attributes
                            AttributeList &= ", " & Att.Name & "=" & Att.Value
                        Next
                        AttributeList = AttributeList.Trim(",")
                        If AttributeList = "" Then
                            name = node.Name
                        Else
                            name = node.Name & "(" & AttributeList & ")"
                        End If
                    End If
            End Select
            newNode.Text = name
            If node.HasChildNodes Then
                PopulateTreeNodeFromXMLNode(newNode, node)
            End If
            control.Nodes.Add(newNode)
        Next
    End Sub

    Private Sub PopulateTreeNodeFromXMLNode(ByRef tnode As TreeNode, ByRef xnode As XmlNode)
        For Each newxnode As XmlNode In xnode
            Dim name As String = ""
            Dim newNode As New TreeNode()
            Select Case newxnode.Name
                Case "#text"
                    newNode.NodeFont = New Font(SystemFonts.MessageBoxFont, FontStyle.Italic)
                    newNode.ForeColor = Color.Red
                    name = newxnode.Name & ": " & newxnode.Value
                Case "#comment"
                    newNode.NodeFont = New Font(SystemFonts.MessageBoxFont, FontStyle.Italic)
                    newNode.ForeColor = Color.Gray
                    name = newxnode.Name & ": " & newxnode.Value
                Case "#cdata-section"
                    newNode.NodeFont = New Font(SystemFonts.MessageBoxFont, FontStyle.Italic)
                    newNode.ForeColor = Color.Red
                    name = newxnode.Name & ": " & newxnode.Value
                Case Else
                    Dim AttributeList As String = ""
                    If Not newxnode Is Nothing Then
                        For Each Att As XmlAttribute In newxnode.Attributes
                            AttributeList &= ", " & Att.Name & "=" & Att.Value
                        Next
                        AttributeList = AttributeList.Trim(",")
                        If AttributeList = "" Then
                            name = newxnode.Name
                        Else
                            name = newxnode.Name & " (" & AttributeList & ")"
                        End If
                    End If
            End Select
            newNode.Text = name
            If newxnode.HasChildNodes Then
                PopulateTreeNodeFromXMLNode(newNode, newxnode)
            End If
            tnode.Nodes.Add(newNode)
        Next
    End Sub
End Class
